package cn.yunyichina.openapi.proxy.utils;

import cn.yunyichina.openapi.proxy.config.HttpServerConfig;
import cn.yunyichina.openapi.proxy.entity.ApiClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class DBUtil {

    private static final Logger logger = LoggerFactory.getLogger(DBUtil.class);

    public static List<ApiClass> getApiClass() {
        List<ApiClass> apiclasses = new ArrayList<>();
        try (Connection con = DriverManager.getConnection(
                HttpServerConfig.getConfig().getJdbcUrl(),
                HttpServerConfig.getConfig().getJdbcUsername(),
                HttpServerConfig.getConfig().getJdbcPassword());
             Statement stmt = con.createStatement()) {
            try {
                Class.forName(HttpServerConfig.getConfig().getJdbcDriverclass()).newInstance();
            } catch (InstantiationException e) {
                logger.error("[DBUtil -> InstantiationException]", e);
            } catch (IllegalAccessException e) {
                logger.error("[DBUtil -> IllegalAccessException]", e);
            } catch (ClassNotFoundException e) {
                logger.error("[DBUtil -> ClassNotFoundException]", e);
            }
            String sql = "select api_class_define, timeout, server_url,port from sys_api_class";
            try (ResultSet rs = stmt.executeQuery(sql)) {
                if (null != rs) {
                    while (rs.next()) {
                        ApiClass apiclass = new ApiClass();
                        apiclass.setApiClassDefine(rs.getString(1));
                        apiclass.setTimeout(rs.getInt(2));
                        apiclass.setServerUrl(rs.getString(3));
                        apiclass.setPort(rs.getInt(4));
                        apiclasses.add(apiclass);
                    }
                }
            }
        } catch (SQLException e) {
            logger.error("[DBUtil -> SQLException]", e);
        }
        return apiclasses;
    }
    
    public static ApiClass getApiClass(String apiClassDefine) {
        List<ApiClass> apiclasses = new ArrayList<>();
        try (Connection con = DriverManager.getConnection(
                HttpServerConfig.getConfig().getJdbcUrl(),
                HttpServerConfig.getConfig().getJdbcUsername(),
                HttpServerConfig.getConfig().getJdbcPassword());
             Statement stmt = con.createStatement()) {
            try {
                Class.forName(HttpServerConfig.getConfig().getJdbcDriverclass()).newInstance();
            } catch (InstantiationException e) {
                logger.error("[DBUtil -> InstantiationException]", e);
            } catch (IllegalAccessException e) {
                logger.error("[DBUtil -> IllegalAccessException]", e);
            } catch (ClassNotFoundException e) {
                logger.error("[DBUtil -> ClassNotFoundException]", e);
            }
            String sql = "select api_class_define, timeout, server_url,port from sys_api_class where api_class_define='"+apiClassDefine+"'";
            try (ResultSet rs = stmt.executeQuery(sql)) {
                if (null != rs) {
                    while (rs.next()) {
                        ApiClass apiclass = new ApiClass();
                        apiclass.setApiClassDefine(rs.getString(1));
                        apiclass.setTimeout(rs.getInt(2));
                        apiclass.setServerUrl(rs.getString(3));
                        apiclass.setPort(rs.getInt(4));
                        apiclasses.add(apiclass);
                    }
                }
            }
        } catch (SQLException e) {
            logger.error("[DBUtil -> SQLException]", e);
        }
        
        if(apiclasses.size()>0)
        	return apiclasses.get(0);
        return null;
    }
    

}
